package com.archetypus.projectschedule.common;


import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


public class ValueListHandler<T> implements ValueListIterator<T> {
	
	protected List<T> list;
	protected ListIterator<T> listIterator;
	
	public ValueListHandler(){}
	
	public void setList(List<T> list) throws IteratorException{
		this.list = list;
		if(list != null)
			listIterator = list.listIterator();
		else
			throw new IteratorException("List empty");
	}
	
	public Collection<T> getList(){
		return list;
	}
	
	
	@Override
	public Object getCurrentElement() throws IteratorException {
		Object obj = null;
		
		if(list != null){
			int currIndex = listIterator.nextIndex();
			obj = list.get(currIndex);
		}else
			throw new IteratorException("List is null");
		
		return obj;
	}

	@Override
	public List<T> getNextElements(int count) throws IteratorException {
		int i = 0;
		T object = null;
		LinkedList<T> list = new LinkedList<T>();
		if(listIterator != null){
			while(  listIterator.hasNext() && (i < count) ){
				object = listIterator.next();
				list.add(object);
				i++;
			}
		}
		else
			throw new IteratorException("List is null"); 

		return list;
	}

	@Override
	public List<T> getPreviousElements(int count) throws IteratorException {
		int i = 0;
		T object = null;
		LinkedList<T> list = new LinkedList<T>();
		if(listIterator != null){
			while(listIterator.hasPrevious() && (i < count)){
				object = listIterator.previous();
				list.add(object);
				i++;
			}
		}else
			throw new IteratorException("List is null");
		
		return list;
	}

	@Override
	public int getSize() throws IteratorException {
		int size = 0;
		
		if(list != null)
			size = list.size();
		else
			throw new IteratorException("List is null");
		
		return size;
	}

	@Override
	public void resetIndex() throws IteratorException {
		if(listIterator != null){
			listIterator = list.listIterator();
		}else
			throw new IteratorException("List is null");
	}

}
